<?php
/**
 * admin 学校管理，以及学校管理员
 */
class Admin_school extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in')) {
            redirect('home');
        }
        //权限检测
        if ($this->session->userdata('r_role_id') != 1) {
            redirect('home');
        }
    }

    /**
     * 管理员的主页面
     * @return [type]
     */
    public function index()
    {
        $school_id = 7;
        //准备数据
        $data = array();
        $data['title'] = '管理员';
        $this->load->model('major_model');
        $this->load->model('school_model');
        $this->load->model('teacher_model');
        $this->load->model('student_model');
        $this->load->model('logic_class_model');
        $teachers_for_check = $this->teacher_model->get_teachers_for_check_by_school_id($school_id);
        $index = 0;
        foreach ($teachers_for_check as $item) {
            $major = $this->major_model->get_by_id($item['major_id']);
            $teachers_for_check[$index]['major_name'] = $major['name'];
            ++$index;
        }
        $data['teachers_for_check'] = $teachers_for_check;
        $data['school'] = $this->school_model->get_by_id($school_id);
        $data['major_count'] = $this->major_model->count_by_school_id($school_id);
        $data['teacher_count'] = $this->teacher_model->count_by_school_id($school_id);
        $data['student_count'] = $this->student_model->count_by_school_id($school_id);
        $data['logic_class_count'] = $this->logic_class_model->count_by_school_id($school_id);
        $this->load->view('admin_school/index', $data);
    }
    //通过教师的申请
    public function teacher_y($teacher_id)
    {
        echo "I am Here";
        $this->load->model('teacher_model');
        $school_id = 7;
        $teacher = $this->teacher_model->get_by_id($teacher_id);
        if (!$teacher) {
            return $this->load->view('error');
        }
        if ($teacher['school_id'] != $school_id) {
            return $this->load->view('error');
        }
        //将内容写入数据库
        $item = array(
            'pass' => 1,
        );
        if ($this->teacher_model->update($teacher_id, $item)) {
            $this->session->set_flashdata('message', '操作成功');
            redirect('admin_school/index');
        } else {
            $this->session->set_flashdata('message', '操作失败');
            redirect('admin_school/index');
        }
    }
    //拒绝教师的申请
    public function teacher_n($teacher_id)
    {
        $this->load->model('teacher_model');
        $this->load->model('user_model');
        $school_id = 7;
        $teacher = $this->teacher_model->get_by_id($teacher_id);
        if (!$teacher) {
            return $this->load->view('error');
        }
        if ($teacher['school_id'] != $school_id) {
            return $this->load->view('error');
        }
        $user = $this->user_model->get_by_id($teacher['user_id']);
        if (!$user) {
            return $this->load->view('error');
        }
        //将教师的注册信息从数据库删除
        if ($this->teacher_model->delete($teacher_id)) {
            if ($this->user_model->delete($teacher['user_id'])) {
                $this->session->set_flashdata('message', '操作成功');
                redirect('admin_school/index');
            } else {
                $this->session->set_flashdata('message', '操作失败');
                redirect('admin_school/index');
            }
        } else {
            $this->session->set_flashdata('message', '操作失败');
            redirect('admin_school/index');
        }
    }

    /**
     * 退出，删除session中的数据
     * @return [type]
     */
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('home');
    }

    /**
     * 修改密码
     * @return [type] [description]
     */
    public function change_password()
    {
        //表单验证
        if ($this->form_validation->run('admin_school/change_password')) {
            //post方法
            //读取数据
            $old_password = $this->input->post('old_password');
            $new_password = $this->input->post('new_password');

            $username = $this->session->userdata('r_username');
            $this->load->model('user_model');
            $user = $this->user_model->check_user_valid($username, $old_password);
            if ($user) {
                $user['password'] = md5($new_password);
                if ($this->user_model->update($user['id'], $user)) {
                    $this->session->set_flashdata('message', '修改成功');
                    redirect('admin_school/index');
                } else {
                    $this->session->set_flashdata('message', '操作失败');
                    redirect('admin_school/index');
                }
            } else {
                $this->session->set_flashdata('message', '密码错误，修改失败');
                redirect('admin_school/index');
            }
        }

        //get方法
        $data['title'] = '修改密码';
        $this->load->view('admin_school/change_password', $data);
    }

    public function editSchool(){
        $data['title'] = '修改学校';
        $this->load->view('admin_school/EditSchoolName', $data);
    }

    public function changeSchool(){
        $schoolName=$this->input->post('schoolName');
        $this->load->model('school_model');
        echo  $schoolName;
        $this->school_model->updateSchool($schoolName);
        redirect('admin_school/index');
    }
}
